﻿using System;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.KDT.GC;
using Company.BLL.DuLieuChuan;
using System.Drawing;
using Company.Interface.GC;
using Company.Interface.KDT.GC;
using Janus.Windows.GridEX;
using Janus.Windows.GridEX.EditControls;
using Company.BLL.KDT;
//using Company.Interface.Reports;

namespace Company.Interface
{    
    public partial class ToKhaiGCChuyenTiepNhapForm : BaseForm
    {
        public ToKhaiChuyenTiep TKCT = new ToKhaiChuyenTiep();
        public Company.BLL.GC.HopDong HDGC = new Company.BLL.GC.HopDong();
        public string NhomLoaiHinh = string.Empty;

        public ToKhaiGCChuyenTiepNhapForm()
        {
            InitializeComponent();            
        }


        private void KhoiTaoGiaoDien()
        {

            txtMaDonVinhan.Text = GlobalSettings.MA_DON_VI;
            txtTenDonVinhan.Text = GlobalSettings.TEN_DON_VI;
            if (this.OpenType == OpenFormType.Edit)
            {
                this.NhomLoaiHinh = this.TKCT.MaLoaiHinh.Trim().Substring(this.TKCT.MaLoaiHinh.Trim().Length - 1, 1);
            }
            if (this.NhomLoaiHinh == "N")
            {
                this.Text += " nhập";
            }
            else 
            {
                this.Text += " xuất";
                grbNguoiGiao.Text = "Người nhận hàng";
                grbNguoiNhan.Text = "Người giao hàng";
                grbHopDongGiao.Text = "Hợp đồng nhận";
                grbHopDongNhan.Text = "Hợp đồng giao";
            }

            cbLoaiHinh.DataSource = LoaiPhieuChuyenTiep.SelectBy_ID("%"+this.NhomLoaiHinh);
            cbLoaiHinh.ValueMember = "ID";
            cbLoaiHinh.DisplayMember = "Ten";
            cbLoaiHinh.SelectedIndex = 0;
            txtMaLoaiHinh.Text = cbLoaiHinh.SelectedValue.ToString();
        }
        private void txtSoHopDongNhan_ButtonClick(object sender, EventArgs e)
        {
            HopDongRegistedForm f = new HopDongRegistedForm();
            f.HopDongSelected.MaHaiQuan = donViHaiQuanControl1.Ma;
            f.IsBrowseFormCT = true;
            f.ShowDialog();      
            if (f.HopDongSelected.SoHopDong != "")
            {
                this.HDGC = f.HopDongSelected;
                txtSoHopDongNhan.Text = f.HopDongSelected.SoHopDong;
                ccNgayHopDongNhan.Value = f.HopDongSelected.NgayKy;
                ccNgayHHHopDongNhan.Value = f.HopDongSelected.NgayHetHan;
            }
        }
        private void loadTKCTData()
        {
            switch (this.TKCT.TrangThai)
            {
                case -1:
                    lbltrangthai.Text = "Chưa gửi đến Hải quan";
                    break;
                case 0:
                    lbltrangthai.Text = "Chờ duyệt chính thức";
                    break;
                case 1:
                    lbltrangthai.Text = "Đã duyệt chính thức";
                    break;
            }
            txtsotiepnhan.Value = this.TKCT.SoTiepNhan;
            donViHaiQuanControl1.Ma = this.TKCT.MaHQ;
            txtToKhaiSo.Value = this.TKCT.ToKhaiSo;
            txtCanbodangky.Text =  this.TKCT.CanBoDangKy;
            ccNgayDangKy.Value = this.TKCT.NgayDangKy;
            txtMaDonVinhan.Text = this.TKCT.MaNguoiNhan;
            txtTenDonVinhan.Text = this.TKCT.TenNguoiNhan;
            txtSoHopDongNhan.Text = this.TKCT.SoHopDongNhan;
            ccNgayHopDongNhan.Value = this.TKCT.NgayHDNhan;
            ccNgayHHHopDongNhan.Value = this.TKCT.NgayHetHanHDNhan;
            txtMaDVGiao.Text = this.TKCT.MaNguoiGiao;
            txtTenDVGiao.Text = this.TKCT.TenNguoiGiao;
            txtSoHongDonggiao.Text = this.TKCT.SoHDGiao;
            ccNgayHopDongGiao.Value = this.TKCT.NgayHDGiao;
            ccNgayHHHopDongGiao.Value = this.TKCT.NgayHetHanHDGiao;
            txtNguoiChiDinhGiaoHang.Text = this.TKCT.NguoiChiDinhGiaoHang;
            txtMaDonViUyThac.Text = this.TKCT.MaDaiLy;
            txtTenDonViUyThac.Text = this.TKCT.TenDaiLy;
            txtMaLoaiHinh.Text = this.TKCT.MaLoaiHinh;
            cbLoaiHinh.SelectedValue = this.TKCT.MaLoaiHinh.Trim();
            txtTyGiaTinhThue.Value = this.TKCT.TyGiaVND;
            txtTyGiaUSD.Value = this.TKCT.TyGiaUSD;
            txtLePhiHQ.Value = this.TKCT.LePhiHQ;
            txtSoBienLai.Text = this.TKCT.SoBienLai;
            ccNgayBienLai.Value = this.TKCT.NgayBienLai;
            txtChungTu.Text = this.TKCT.ChungTu;
            txtDiaDiemXepHang.Text = this.TKCT.DiaDiemXepHang;
            nguyenTeControl1.Ma = this.TKCT.MaNguyenTe;
            this.TKCT.LoadHCTCollection();
            dgList.DataSource = this.TKCT.HCTCollection;
            donViHaiQuanControl1.ReadOnly = true;
            ccNgayDangKy.ReadOnly = true;
            cbLoaiHinh.ReadOnly = true;
        }
        private void ToKhaiGCChuyenTiepNhapForm_Load(object sender, EventArgs e)
        {
            KhoiTaoGiaoDien();
            switch (this.OpenType)
            {
                case OpenFormType.Edit:
                    this.loadTKCTData();
                    break;
                case OpenFormType.View:
                    this.loadTKCTData();
                    this.ViewData();
                    break;
            }
            dgList.DataSource = TKCT.HCTCollection;
        }

        private void ViewData()
        {
            cmdThemHang.Enabled = Janus.Windows.UI.InheritableBoolean.False;
            cmdSave.Enabled = Janus.Windows.UI.InheritableBoolean.False;
            cmdSend.Enabled = Janus.Windows.UI.InheritableBoolean.False;
            txtToKhaiSo.ReadOnly = true;
            txtCanbodangky.ReadOnly = true;
            ccNgayDangKy.ReadOnly = true;
            txtMaDonVinhan.ReadOnly = true;
            txtTenDonVinhan.ReadOnly = true;
            txtSoHopDongNhan.ReadOnly = true;
            txtSoHopDongNhan.ButtonStyle = EditButtonStyle.NoButton;
            ccNgayHopDongNhan.ReadOnly = true;
            ccNgayHHHopDongNhan.ReadOnly = true;
            txtMaDVGiao.ReadOnly = true;
            txtTenDVGiao.ReadOnly = true;
            txtSoHongDonggiao.ReadOnly = true;
            ccNgayHopDongGiao.ReadOnly = true;
            ccNgayHHHopDongGiao.ReadOnly = true;
            txtNguoiChiDinhGiaoHang.ReadOnly = true;
            txtMaDonViUyThac.ReadOnly = true;
            txtTenDonViUyThac.ReadOnly = true;
            cbLoaiHinh.ReadOnly = true;
            txtTyGiaTinhThue.ReadOnly = true;
            txtTyGiaUSD.ReadOnly = true;
            txtLePhiHQ.ReadOnly = true;
            txtSoBienLai.ReadOnly = true;
            ccNgayBienLai.ReadOnly = true;
            txtChungTu.ReadOnly = true;
            txtDiaDiemXepHang.ReadOnly = true;
            nguyenTeControl1.ReadOnly = true;
        }

        private void cbLoaiHinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMaLoaiHinh.Text = cbLoaiHinh.SelectedValue.ToString();
            if (this.OpenType == OpenFormType.Insert)
            {
                this.TKCT.HCTCollection.Clear();
                dgList.Refetch();
            }

        }

        private void cmMain_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
        {
            switch (e.Command.Key)
            {
                case "cmdSave":
                    this.Save();
                    break;
                case "cmdThemHang":
                    this.Add();
                    break;
                case "cmdPrint":
                    this.Print();
                    break;
                case "cmdSend":
                    this.Send();
                    break;
            }
        }

        private void Send()
        {
            cvError.Validate();
            if (cvError.IsValid)
            {
                if (this.TKCT.HCTCollection.Count == 0)
                {
                    ShowMessage("Chưa nhập thông tin hàng của tờ khai", false);
                    return;
                }
                
                if (this.TKCT.ID == 0)
                {
                    this.TKCT.TrangThai = TrangThaiXuLy.CHUA_KHAI_BAO;
                    this.TKCT.SoTiepNhan = 0;
                    this.TKCT.NgayTiepNhan = new DateTime(1900, 01, 01);
                }

                this.TKCT.MaHQ = donViHaiQuanControl1.Ma;
                this.TKCT.ToKhaiSo = Convert.ToInt32(txtToKhaiSo.Value);
                this.TKCT.CanBoDangKy = txtCanbodangky.Text;
                this.TKCT.NgayDangKy = ccNgayDangKy.Value;
                this.TKCT.MaNguoiNhan = txtMaDonVinhan.Text;
                this.TKCT.TenNguoiNhan = txtTenDonVinhan.Text;
                this.TKCT.SoHopDongNhan = txtSoHopDongNhan.Text;
                this.TKCT.NgayHDNhan = ccNgayHopDongNhan.Value;
                this.TKCT.NgayHetHanHDNhan = ccNgayHHHopDongNhan.Value;
                this.TKCT.MaNguoiGiao = txtMaDVGiao.Text;
                this.TKCT.TenNguoiGiao = txtTenDVGiao.Text;
                this.TKCT.SoHDGiao = txtSoHongDonggiao.Text;
                this.TKCT.NgayHDGiao = ccNgayHopDongGiao.Value;
                this.TKCT.NgayHetHanHDGiao = ccNgayHHHopDongGiao.Value;
                this.TKCT.NguoiChiDinhGiaoHang = txtNguoiChiDinhGiaoHang.Text;
                this.TKCT.MaDaiLy = txtMaDonViUyThac.Text;
                this.TKCT.TenDaiLy = txtTenDonViUyThac.Text;
                this.TKCT.MaLoaiHinh = txtMaLoaiHinh.Text;
                this.TKCT.TenLoaiHinh = cbLoaiHinh.Text;
                this.TKCT.TyGiaVND = (decimal)txtTyGiaTinhThue.Value;
                this.TKCT.TyGiaUSD = (decimal)txtTyGiaUSD.Value;
                this.TKCT.LePhiHQ = (decimal)txtLePhiHQ.Value;
                this.TKCT.SoBienLai = txtSoBienLai.Text;
                this.TKCT.NgayBienLai = ccNgayBienLai.Value;
                this.TKCT.ChungTu = txtChungTu.Text;
                this.TKCT.DiaDiemXepHang = txtDiaDiemXepHang.Text;
                this.TKCT.MaNguyenTe = nguyenTeControl1.Ma;
                refresh_STTHang();
                try
                {
                    WSForm wsForm = new WSForm();
                    wsForm.ShowDialog(this);
                    if (!wsForm.IsReady) return;
                    this.TKCT.WSSend();
                    if (this.TKCT.SoTiepNhan == 0)
                    {
                        ShowMessage("Khai báo không thành công", true);

                    }
                    else
                    {
                        ShowMessage("Khai báo thành công!\nSố tiếp nhận đăng ký điện tử: " + this.TKCT.SoTiepNhan, false);
                        dgList.Refetch();
                    }
                    
                }
                catch (Exception ex)
                {
                    if (ShowMessage("Khai báo không thành công.\nLỗi: " + ex.Message + ".\nBạn có muốn đưa vào hàng đợi không?", true) == "Yes")
                    {
                        HangDoi hd = new HangDoi();
                        hd.ID = this.TKCT.ID;
                        hd.LoaiToKhai = LoaiToKhai.TO_KHAI_CHUYEN_TIEP;
                        hd.TrangThai = this.TKCT.TrangThai;
                        hd.ChucNang = ChucNang.KHAI_BAO;
                        MainForm.AddToQueueForm(hd);
                        MainForm.ShowQueueForm();
                    }

                }
                finally
                {
                    this.Cursor = Cursors.Default;
                }
                
            }
        }
        private void Print()
        {
 
        }
        private void Add()
        {
            if (txtSoHopDongNhan.Text == "")
            {
                epError.SetError(txtSoHopDongNhan, "Số hợp đồng nhận không được rỗng.");
                epError.SetIconPadding(txtSoHopDongNhan, -8);
                return;
            }
            HangChuyenTiepForm f = new HangChuyenTiepForm();
            f.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            if (this.HDGC.SoHopDong == "")
            {
                this.HDGC.SoHopDong = txtSoHopDongNhan.Text;
                this.HDGC.NgayKy = ccNgayHopDongNhan.Value;
                this.HDGC.MaDoanhNghiep = txtMaDonVinhan.Text;
                this.HDGC.MaHaiQuan = donViHaiQuanControl1.Ma;
            }
            f.HD = this.HDGC;
            f.MaLoaiHinh = txtMaLoaiHinh.Text;
            f.MaNguyenTe = nguyenTeControl1.Ma;
            f.HCTCollection = this.TKCT.HCTCollection;
            f.ShowDialog();
            this.TKCT.HCTCollection = f.HCTCollection;
            dgList.DataSource = this.TKCT.HCTCollection;
            refresh_STTHang();
            dgList.Refetch();
        }
        private void Save()
        {
            cvError.Validate();
            if (cvError.IsValid)
            {
                if (this.TKCT.HCTCollection.Count == 0)
                {
                    ShowMessage("Chưa nhập thông tin hàng của tờ khai", false);
                    return;
                }
                this.TKCT.SoTiepNhan = Convert.ToInt32(txtsotiepnhan.Text);

                if (this.OpenType == OpenFormType.Edit)
                    this.TKCT.TrangThai = TrangThaiXuLy.CHO_DUYET;
                else
                {
                    this.TKCT.TrangThai = TrangThaiXuLy.CHUA_KHAI_BAO;
                    this.TKCT.NgayTiepNhan = new DateTime(1900, 01, 01);
                }
                this.TKCT.MaHQ = donViHaiQuanControl1.Ma;
                this.TKCT.ToKhaiSo = Convert.ToInt32(txtToKhaiSo.Value);
                this.TKCT.CanBoDangKy = txtCanbodangky.Text;
                this.TKCT.NgayDangKy = ccNgayDangKy.Value;
                this.TKCT.MaNguoiNhan = txtMaDonVinhan.Text;
                this.TKCT.TenNguoiNhan = txtTenDonVinhan.Text;
                this.TKCT.SoHopDongNhan = txtSoHopDongNhan.Text;
                this.TKCT.NgayHDNhan = ccNgayHopDongNhan.Value;
                this.TKCT.NgayHetHanHDNhan = ccNgayHHHopDongNhan.Value;
                this.TKCT.MaNguoiGiao = txtMaDVGiao.Text;
                this.TKCT.TenNguoiGiao = txtTenDVGiao.Text;
                this.TKCT.SoHDGiao = txtSoHongDonggiao.Text;
                this.TKCT.NgayHDGiao = ccNgayHopDongGiao.Value;
                this.TKCT.NgayHetHanHDGiao = ccNgayHHHopDongGiao.Value;
                this.TKCT.NguoiChiDinhGiaoHang = txtNguoiChiDinhGiaoHang.Text;
                this.TKCT.MaDaiLy = txtMaDonViUyThac.Text;
                this.TKCT.TenDaiLy = txtTenDonViUyThac.Text;
                this.TKCT.MaLoaiHinh = txtMaLoaiHinh.Text;
                this.TKCT.TenLoaiHinh = cbLoaiHinh.Text;
                this.TKCT.TyGiaVND = (decimal)txtTyGiaTinhThue.Value;
                this.TKCT.TyGiaUSD = (decimal)txtTyGiaUSD.Value;
                this.TKCT.LePhiHQ = (decimal)txtLePhiHQ.Value;
                this.TKCT.SoBienLai = txtSoBienLai.Text;
                this.TKCT.NgayBienLai = ccNgayBienLai.Value;
                this.TKCT.ChungTu = txtChungTu.Text;
                this.TKCT.DiaDiemXepHang = txtDiaDiemXepHang.Text;
                this.TKCT.MaNguyenTe = nguyenTeControl1.Ma;
                refresh_STTHang();
                long ret;
                if (this.TKCT.ID == 0)
                {
                    ret = this.TKCT.InsertFull();
                    if (ret > 0)
                    {
                        this.TKCT.ID = ret;
                        this.TKCT.LoadHCTCollection();
                        ShowMessage("Ghi thành công", false);
                    }
                }
                else
                {
                    ret = this.TKCT.UpdateFull();
                    if (ret > 0)
                    {
                        ShowMessage("Ghi thành công", false);
                    }
                }
                dgList.Refetch();
            }
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            e.Row.Cells["ID_NuocXX"].Text = this.Nuoc_GetName(e.Row.Cells["ID_NuocXX"].Text.ToString());
            e.Row.Cells["ID_DVT"].Text = this.DonViTinh_GetName(e.Row.Cells["ID_DVT"].Text.ToString());
        }
        private void refresh_STTHang()
        {
            int i = 1;
            foreach (HangChuyenTiep hct in this.TKCT.HCTCollection)
            {
                hct.SoThuTuHang = i++;
            }
        }
        private void dgList_DeletingRecord(object sender, RowActionCancelEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (ShowMessage("Bạn có muốn xóa hàng hóa này không?", true) == "Yes")
                {
                    HangChuyenTiep hct = (HangChuyenTiep)e.Row.DataRow;
                    if (hct.ID > 0)
                    {
                        hct.Delete();
                        
                    }

                }
                else
                {
                    e.Cancel = true;
                }
            }
           
        }
        private int getPostion(HangChuyenTiep hct)
        {
            for (int i = 0; i < this.TKCT.HCTCollection.Count; i++)
            {
                if (this.TKCT.HCTCollection[i].MaHang == hct.MaHang) return i;
            }
            return -1;
        }
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (e.Row.RowType== RowType.Record)
            {   
                HangChuyenTiepEditForm f = new HangChuyenTiepEditForm();
                f.HCT= (HangChuyenTiep)e.Row.DataRow;
                int i = this.getPostion(f.HCT);
                f.HCTCollection = this.TKCT.HCTCollection;
                f.HCTCollection.RemoveAt(i);
                if (this.HDGC.SoHopDong == "")
                {
                    this.HDGC.SoHopDong = TKCT.SoHopDongNhan;
                    this.HDGC.MaDoanhNghiep = TKCT.MaNguoiNhan;
                    this.HDGC.MaHaiQuan = TKCT.MaHQ;
                    this.HDGC.NgayKy = TKCT.NgayHDNhan;
                }
                f.HD = this.HDGC;
                f.MaLoaiHinh = this.txtMaLoaiHinh.Text;
                f.MaHaiQuan = this.donViHaiQuanControl1.Ma;
                f.MaNguyenTe = nguyenTeControl1.Ma;
                f.ShowDialog();
                if (f.IsEdited)
                {
                    if (f.HCT.Master_ID > 0)
                        f.HCT.Update();
                    this.TKCT.HCTCollection.Insert(i,f.HCT);
                }
                else if (f.IsDeleted)
                {
                    if (f.HCT.Master_ID > 0)
                        f.HCT.Delete();
                    //this.TKCT.HCTCollection.RemoveAt(i.Position);
                }
                dgList.DataSource = this.TKCT.HCTCollection;
                dgList.Refetch();
            }
            
        }
    }
}